package com.dc.common.vo;


import com.dc.common.constant.ResultStatus;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author zhuangcy
 * @date 2021/1/16
 * @description 请求响应Vo类
 */
@ApiModel("请求响应Vo类")
@Data
public class ResultVo<T> {

    @ApiModelProperty("响应码")
    private int code;

    @ApiModelProperty("响应消息")
    private String msg;

    @ApiModelProperty("是否成功")
    private boolean success;

    @ApiModelProperty("响应数据")
    private T data;

    public ResultVo(int code, String msg, boolean success, T data) {
        this.code = code;
        this.msg = msg;
        this.success = success;
        this.data = data;
    }

    /**
     * 响应成功
     *
     * @return
     */
    public static ResultVo<Object> success() {
        return new ResultVo<>(ResultStatus.SUCCESS_CODE.getCode(), ResultStatus.SUCCESS_CODE.getMsg(), true, null);
    }

    /**
     * 响应失败
     *
     * @return
     */
    public static ResultVo<Object> failed() {
        return new ResultVo<>(ResultStatus.FAIL_CODE.getCode(), ResultStatus.FAIL_CODE.getMsg(), false, null);
    }

    /**
     * 设置返回响应数据
     *
     * @param data
     * @return
     */
    public ResultVo<T> data(T data) {
        this.data = data;
        return this;
    }

    /**
     * 设置响应请求信息
     *
     * @param msg
     * @return
     */
    public ResultVo<T> msg(String msg) {
        this.msg = msg;
        return this;
    }

    /**
     * 设置响应编码
     *
     * @param code
     * @return
     */
    public ResultVo<T> code(int code) {
        this.code = code;
        return this;
    }

    /**
     * 校验
     *
     * @param flag
     * @return
     */
    public static ResultVo verify(boolean flag) {
        if (flag) {
            return ResultVo.success();
        } else {
            return ResultVo.failed();
        }
    }

}
